﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class QuanLyThi
    {
        #region thuoc tinh
        string _MSSV;

        public string MSSV
        {
            get { return _MSSV; }
            set { _MSSV = value; }
        }
        int _MaD;

        public int MaD
        {
            get { return _MaD; }
            set { _MaD = value; }
        }
        float _Diem;

        public float Diem
        {
            get { return _Diem; }
            set { _Diem = value; }
        }
        #endregion
        public QuanLyThi()
        { }
        public QuanLyThi(string ma, int mad, float die)
        {
            MSSV = ma;
            MaD = mad;
            Diem = die;
        }
        private static SqlConnection conn;
        private static SqlCommand cmd;

        private static QuanLyThi GetData(SqlDataReader reader)
        {
            QuanLyThi data = new QuanLyThi();
            data.MSSV = CType.Tostring(reader[0]);
            data.MaD = CType.ToInt32(reader[1]);
            data.Diem = CType.ToInt32(reader[2]);
            return data;
        }
        public static List<QuanLyThi> LayDiemThiSV(string mssv)
        {
            List<QuanLyThi> list = new List<QuanLyThi>();
            using (conn = MyConnection.MyConn())
            {
                cmd = MyCommand.Command("QuanLyDangKyHocPhan_XemDiem", conn);
                cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    QuanLyThi data = GetData(reader);
                    list.Add(data);
                }
                return list;
            }
        }

        public static float QuanLyThi_XemDiemThiLan2(string mssv, string mamh, int mahk, string namhoc)
        {
            try
            {
                using (conn = MyConnection.MyConn())
                {
                    cmd = MyCommand.Command("QuanLyThi_XemDiemThiLan2", conn);
                    cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mamh;
                    cmd.Parameters.Add("@MaHK", SqlDbType.Int).Value = mahk;
                    cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 9).Value = namhoc;

                    conn.Open();
                    float rs = float.Parse(cmd.ExecuteScalar().ToString());

                    return rs;
                }
            }
            catch
            {
                return -1;
            }

        }
        public static float QuanLyThi_LayTiLeThiLan2(string mssv, string mamh, int mahk, string namhoc)
        {
            try
            {
                using (conn = MyConnection.MyConn())
                {
                    cmd = MyCommand.Command("QuanLyThi_LayTiLeThiLan2", conn);
                    cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mamh;
                    cmd.Parameters.Add("@MaHK", SqlDbType.Int).Value = mahk;
                    cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 9).Value = namhoc;

                    conn.Open();
                    float rs = float.Parse(cmd.ExecuteScalar().ToString());
                    return rs;
                }
            }
            catch
            {
                return -1;
            }

        }
        public static float QuanLyThi_XemDiemThiLan2_100(string mssv, string mamh, int mahk, string namhoc)
        {
            try
            {
                using (conn = MyConnection.MyConn())
                {
                    cmd = MyCommand.Command("QuanLyThi_XemDiemThiLan2_100", conn);
                    cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mamh;
                    cmd.Parameters.Add("@MaHK", SqlDbType.Int).Value = mahk;
                    cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 9).Value = namhoc;

                    conn.Open();
                    float rs = float.Parse(cmd.ExecuteScalar().ToString());
                    return rs;
                }
            }
            catch
            {
                return -1;
            }

        }
        public static float QuanLyThi_XemDiemThiLan1(string mssv, string mamh, int mahk, string namhoc)
        {
            try
            {
                using (conn = MyConnection.MyConn())
                {
                    cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "QuanLyThi_XemDiemThiLan1";
                    cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mamh;
                    cmd.Parameters.Add("@MaHK", SqlDbType.Int).Value = mahk;
                    cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 9).Value = namhoc;

                    conn.Open();
                    float rs = float.Parse(cmd.ExecuteScalar().ToString());
                    return rs;
                }
            }
            catch
            {
                return -1;
            }

        }
        public static int QuanLyThi_CapNhatDiem(string mssv,int maD,int Diem)
        {
            List<QuanLyThi> list = new List<QuanLyThi>();
            using (conn = MyConnection.MyConn())
            {
                cmd = MyCommand.Command("QuanLyThi_CapNhatDiem", conn);
                cmd.Parameters.Add("@MSSV", SqlDbType.VarChar, 10).Value = mssv;
                cmd.Parameters.Add("@MaD", SqlDbType.Int).Value = maD;
                cmd.Parameters.Add("@Diem", SqlDbType.Int).Value = Diem;
                conn.Open();
                int rs = cmd.ExecuteNonQuery();
                return rs;
            }
        }
    }
}
